<?php
/**
*
* @package ppkBB3cker
* @version $Id: ajax.php 1.000 2011-01-30 15:11:00 PPK $
* @copyright (c) 2011 PPK
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin(false);
$auth->acl($user->data);
$user->setup();

if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']))
{
	show_result('Error [6]');
}

$post_id	= request_var('p', 0);
$opt	= request_var('opt', '');
$page	= request_var('page', '');
$mua=request_var('mua', '');
$adm	= request_var('adm', 0);

sleep(1);

$ajax_root_path=$adm && $auth->acl_get('a_') && !empty($user->data['is_registered']) ? './../' : './';
$i3=$forum_astracker=$mua_user_id=0;
$dt=time();
$sort_opt=$postrow=$assign_vars=array();
$invalid_opt=true;
if(!in_array($page, array('vt', 'mua')) && in_array($opt, array('say_thanks', 'seed_request', 'set_sticky', 'set_forb')))
{
	$invalid_opt=false;
}
if($page=='vt' && in_array($opt, array('filelist', 'finished', 'seed', 'leech', 'thanks', 'history', 'leave', 'seedreq', 'remote')))
{
	$viewtopic_add1inc=$phpbb_root_path.'tracker/include/';
	$invalid_opt=false;
}
if($page=='mua' && in_array($opt, array('torrent', 'finished', 'seed', 'leech', 'tothanks', 'fromthanks', 'history', 'leave', 'fromseedreq')))
{
	$mua_add1inc=$phpbb_root_path.'tracker/include/';
	$mua=='ucp' ? $mua_user_id=$user->data['user_id'] : $mua_user_id=my_int_val($mua);
	$invalid_opt=$adm && !$auth->acl_gets('a_') ? true : false;
	!$auth->acl_gets('u_viewprofile', 'a_user', 'a_useradd', 'a_userdel') && $mua!='ucp' ? $invalid_opt=true : '';
}
if($invalid_opt)
{
	show_result('Invalid opt [0]');
}

$user->add_lang('viewtopic');
$ppkbb3cker_addons=generate_board_url()."/tracker/addons/";
$ppkbb_tcguests_enabled=my_split_config($config['ppkbb_tcguests_enabled'], 2, 'my_int_val');
$ppkbb_tcrewr_updown=my_split_config($config['ppkbb_tcrewr_updown'], 3, 'my_int_val');
$ppkbb_tcenable_rannounces=my_split_config($config['ppkbb_tcenable_rannounces'], 9, 'my_int_val');
!$user->data['is_registered'] && !$ppkbb_tcguests_enabled[0] ? $torrent_guest=0 : $torrent_guest=1;
$is_admod=$auth->acl_gets('a_', 'm_') || $auth->acl_getf_global('m_') ? 1 : 0;

if($page!='mua')
{
	$sql_array = array(
		'SELECT' => 't.*, f.*, p.post_id, p.post_approved, p.poster_id',
		'FROM'	=> array(
			FORUMS_TABLE => 'f',
			POSTS_TABLE => 'p',
			TOPICS_TABLE => 't',
		),
		'WHERE' => "p.post_id = {$post_id} AND t.topic_id = p.topic_id AND f.forum_id = t.forum_id",
	);

	$sql = $db->sql_build_query('SELECT', $sql_array);
	$result = $db->sql_query($sql);
	$topic_data = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);

	$forum_id = (int) $topic_data['forum_id'];
	$topic_id = (int) $topic_data['topic_id'];
	$post_id = 	(int) $topic_data['post_id'];
	$poster_id = 	(int) $topic_data['poster_id'];

	if(!$topic_data || (!$topic_data['post_approved'] && !$auth->acl_get('m_approve', $topic_data['forum_id'])) || !$auth->acl_get('f_read', $forum_id))
	{
		show_result('Invalid permissions (Global)');
	}

	$forum_astracker=($topic_data['forumas']==1 && $topic_data['topic_type']!=POST_GLOBAL) ? 1 : 0;
}
else
{
	// Get user...
	$sql = 'SELECT *
		FROM ' . USERS_TABLE . "
		WHERE  user_id = {$mua_user_id}";
	$result = $db->sql_query($sql);
	$member = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);

	if (!$member)
	{
		show_result($user->lang['NO_USER']);
	}

	// a_user admins and founder are able to view inactive users and bots to be able to manage them more easily
	// Normal users are able to see at least users having only changed their profile settings but not yet reactivated.
	if (!$auth->acl_get('a_user') && $user->data['user_type'] != USER_FOUNDER)
	{
		if ($member['user_type'] == USER_IGNORE)
		{
			show_result($user->lang['NO_USER']);
		}
		else if ($member['user_type'] == USER_INACTIVE && $member['user_inactive_reason'] != INACTIVE_PROFILE)
		{
			show_result($user->lang['NO_USER']);
		}
	}
}

if($forum_astracker)
{
	$sql='SELECT tr.id torrent_id, tr.times_completed'.($ppkbb_tcenable_rannounces[0] ? '+tr.rem_times_completed' : '').' times_completed, tr.leechers'.($ppkbb_tcenable_rannounces[0] ? '+tr.rem_leechers' : '').' leechers, tr.seeders'.($ppkbb_tcenable_rannounces[0] ? '+tr.rem_seeders' : '').' seeders, tr.size, tr.free, tr.upload, tr.forb, tr.forb_reason, tr.forb_user_id, tr.forb_date, tr.tsl_speed, tr.added, tr.lastseed, tr.lastleech, tr.private, tr.req_ratio, tr.req_upload, tr.info_hash, tr.lastcleanup, tr.rem_leechers, tr.rem_seeders, tr.poster_id, tr.rem_times_completed, tr.lastremote, tr.lastseedreq, tt.id thanks, u.username, u.user_colour FROM '.TRACKER_TORRENTS_TABLE ." tr LEFT JOIN ". POSTS_TABLE ." p ON (p.topic_id=tr.topic_id AND p.post_id=tr.post_msg_id) LEFT JOIN ".TRACKER_THANKS_TABLE." tt ON (tt.torrent_id=tr.id AND tt.user_id='{$user->data['user_id']}') LEFT JOIN ".USERS_TABLE." u ON (tr.forb_user_id=u.user_id) WHERE tr.topic_id='{$topic_data['topic_id']}' LIMIT 1";
	$result=$db->sql_query($sql);
	while($row_ajax=$db->sql_fetchrow($result))
	{
		if(@$row_ajax['torrent_id'])
		{
			$torrents[$row_ajax['torrent_id']]=$row_ajax;
			$torrent_id=$row_ajax['torrent_id'];
		}
	}
	$db->sql_freeresult($result);

	$is_candownload=$auth->acl_get('u_download') && $auth->acl_get('f_download', $forum_id) ? 1 : 0;
	if(!$user->data['is_registered'])
	{
		$is_candowntorr = $auth->acl_get('u_candowntorr') && $auth->acl_get('f_candowntorr', $forum_id) ? 1 : 0;
		$is_candownpostscr = $auth->acl_get('u_candownpostscr') && $auth->acl_get('f_candownpostscr', $forum_id) ? 1 : 0;
	}
	else
	{
		$is_candowntorr=$is_candownpostscr=1;
		if($user->data['user_id']!=$topic_data['topic_poster'])
		{
			$is_candowntorr=$auth->acl_get('u_candowntorr') && $auth->acl_get('f_candowntorr', $forum_id) ? 1 : 0;
			$is_candownpostscr=$auth->acl_get('u_candownpostscr') && $auth->acl_get('f_candownpostscr', $forum_id) ? 1 : 0;
		}
	}
	$is_canusethanks = ($auth->acl_get('u_canusethanks') && $auth->acl_get('f_canusethanks', $forum_id)) ? 1 : 0;
	!$is_candowntorr ? $is_canusethanks=0 : '';
	$torrent_link=1;
	$ppkbb_tcauthor_candown=my_split_config($config['ppkbb_tcauthor_candown'], 0 , 'intval');
	$torrents[$torrent_id]['forb'] < 1 || ($user->data['user_id']==$poster_id && in_array($torrents[$torrent_id]['forb'], $ppkbb_tcauthor_candown)) ? '' : $torrent_link=0;

	$is_canskiprcheck = $user->data['is_registered'] && $user->data['user_id']!=$poster_id ? (($auth->acl_get('u_canskiprcheck') && $auth->acl_get('f_canskiprcheck', $forum_id)) ? 1 : 0) : 1;
	$is_canskiprequpload = $user->data['is_registered'] && $user->data['user_id']!=$poster_id ? (($auth->acl_get('u_canskiprequpload') && $auth->acl_get('f_canskiprequpload', $forum_id)) ? 1 : 0) : 1;
	$is_canskipreqratio = $user->data['is_registered'] && $user->data['user_id']!=$poster_id ? (($auth->acl_get('u_canskipreqratio') && $auth->acl_get('f_canskipreqratio', $forum_id)) ? 1 : 0) : 1;

	$ppkbb_tcguests_enabled=my_split_config($config['ppkbb_tcguests_enabled'], 2, 'my_int_val');
	$ppkbb_tcrewr_updown=my_split_config($config['ppkbb_tcrewr_updown'], 3, 'my_int_val');
	$ppkbb_tcenable_rannounces=my_split_config($config['ppkbb_tcenable_rannounces'], 9, 'my_int_val');

	$user_ratio=get_ratio($user->data['user_uploaded'], $user->data['user_downloaded'], $config['ppkbb_tcratio_start'], $user->data['user_bonus']);
	$t_elapsed = intval(($dt - @$torrents[$torrent_id]['added']) / 3600);
	$t_wait=-1;
	if(!$ppkbb_tcrewr_updown[2] && !$is_canskiprcheck && $config['ppkbb_tcwait_time'])
	{
		$t_wait=get_trestricts($user->data['user_uploaded'], $user->data['user_downloaded'], $user_ratio, $config['ppkbb_tcwait_time'], 'up');

		if ($t_wait > 0)
		{
			$t_elapsed < $t_wait ? $t_wait=$t_wait - $t_elapsed : $t_wait=-1;
		}
		$t_wait >= 0 ? $torrent_link=0 : '';
	}

	$t_wait2=-1;
	if(!$ppkbb_tcrewr_updown[2] && !$is_canskiprcheck && $config['ppkbb_tcwait_time2'])
	{
		$t_wait2=get_trestricts($user->data['user_uploaded'], $user->data['user_downloaded'], $user_ratio, $config['ppkbb_tcwait_time2']);
		if ($t_wait2 > 0)
		{
			$t_elapsed < $t_wait2 ? $t_wait2=$t_wait2 - $t_elapsed : $t_wait2=-1;
		}
		$t_wait2 >= 0 ? $torrent_link=0 : '';
	}
	(!$is_candowntorr || !$torrent_guest/* || !$torrent_link*/ || (!$ppkbb_tcenable_rannounces[0] && $opt=='remote')) && $page ? $opt='' : '';
}
if($page!='mua' && !$forum_astracker)
{
	show_result('Not in a tracker');
}

switch($opt)
{
	case 'filelist':
	case 'finished':
	case 'seed':
	case 'leech':
	case 'thanks':
	case 'history':
	case 'leave':
	case 'seedreq':
	case 'remote':

	case 'torrent':
	case 'tothanks':
	case 'fromthanks':
	case 'fromseedreq':

		//if($page!=$vt)
		//{
			$ex_fid_ary=array_keys($auth->acl_getf('!f_read', true));
		//}
		page_header();
		$template->set_filenames(array(
			'body' => $page=='vt' ? 'viewtopic_tracker_body_explain.html' : 'mua_torrents_explain.html')
		);
		include(($page=='vt' ? "{$viewtopic_add1inc}viewtopic" : "{$mua_add1inc}mua")."_add1_{$opt}.{$phpEx}");

		$template->assign_vars(array(
			'S_HAS_TORRENT_EXPLAIN_'.strtoupper($opt)	=> true,
			'S_HAS_TORRENT_EXPLAIN' => true,
			'PPKBB3CKER_ADDONS'	=> $ppkbb3cker_addons,
			)
		);

		if(sizeof($assign_vars))
		{
			foreach($assign_vars as $k2 => $v2)
			{
				$template->assign_block_vars($torrent_info_curr.'_option', $v2);
			}
		}
		if(sizeof($postrow_headers))
		{
			foreach($postrow_headers as $k2 => $v2)
			{
				$template->assign_block_vars('headers', array('VALUE' => $v2));
			}
		}
		if(sizeof($postrow_header))
		{
			$template->assign_var('S_TORRENT_FOOTER', implode(' : ', $postrow_header));
		}
		page_footer();
	break;

	case 'say_thanks':
		if(!$torrents[$torrent_id]['thanks'] && $torrents[$torrent_id]['poster_id']!=$user->data['user_id'] && $is_canusethanks && $torrent_link)
		{
			$say_thanks=request_var('say_thanks', 0);
			//!$say_thanks ? $say_thanks=(request_var('submit_thanks_x', '') || request_var('submit_thanks_y', '')) : '';
			if($say_thanks)
			{
				$db->sql_query('INSERT IGNORE INTO '. TRACKER_THANKS_TABLE ." VALUES (0, '{$user->data['user_id']}', '{$torrent_id}', '{$torrents[$torrent_id]['poster_id']}', '{$dt}')");
				if($db->sql_affectedrows())
				{
				$db->sql_query("UPDATE ".USERS_TABLE." SET user_tothanks_count=user_tothanks_count+1 WHERE user_id='{$torrents[$torrent_id]['poster_id']}'");
				$db->sql_query("UPDATE ".USERS_TABLE." SET user_fromthanks_count=user_fromthanks_count+1 WHERE user_id='{$user->data['user_id']}'");
				show_result($user->lang['TORR_THANKS_SEND'], $opt);
			}
		}
		}
		else
		{
			show_result('Invalid permissions (Thanks)');
		}
	break;

	case 'seed_request':
		$ppkbb_tcenable_rannounces=my_split_config($config['ppkbb_tcenable_rannounces'], 9, 'my_int_val');

		$seed_percent=0;
		if($torrents[$torrent_id]['seeders']-($config['ppkbb_srenable']==1 ? $torrents[$torrent_id]['rem_seeders'] : 0) < $torrents[$torrent_id]['times_completed']-($config['ppkbb_srenable']==1 ? $torrents[$torrent_id]['rem_times_completed'] : 0)+1)
		{
			$seed_percent=my_int_val(($torrents[$torrent_id]['seeders']-($config['ppkbb_srenable']==1 ? $torrents[$torrent_id]['rem_seeders'] : 0)) * 100 / ($torrents[$torrent_id]['times_completed']-($config['ppkbb_srenable']==1 ? $torrents[$torrent_id]['rem_times_completed'] : 0)+1));
		}
		else
		{
			$seed_percent=100;
		}

		$sr_addon=array();
		$is_canuseseedreq = ($auth->acl_get('u_canuseseedreq') && $auth->acl_get('f_canuseseedreq', $forum_id) && $user->data['user_id']!=$poster_id) && $user->data['is_registered'] ? 1 : 0;
		$ppkbb_srincl_ratio=my_split_config($config['ppkbb_srincl_ratio']);

		if($config['ppkbb_srenable'] && $is_canuseseedreq && $torrents[$torrent_id]['forb'] < 1)
		{
			if($torrents[$torrent_id]['size'] > $config['ppkbb_srfile_size']*1024*1024 && ((sizeof($ppkbb_srincl_ratio) && in_array($user_ratio, $ppkbb_srincl_ratio)) || $user_ratio > $config['ppkbb_srreq_ratio']) && ($seed_percent <= $config['ppkbb_srseed_percent'] || !$config['ppkbb_srseed_percent']) && $torrent_link)
			{
				$d_seedreq=0;
				$ppkbb_srreq_time=intval($config['ppkbb_srreq_time'] * 60 * 60);
				$ppkbb_srreq_time ? '' : $ppkbb_srreq_time=600;
				if($ppkbb_srreq_time)
				{
					/*$sql="SELECT added FROM ".TRACKER_SEEDREQUEST_TABLE." WHERE (user_id='{$user->data['user_id']}' OR torrent='{$torrent_id}') AND $dt - added < {$ppkbb_srreq_time} LIMIT 1";
					$result=$db->sql_query($sql);
					$d_seedreq=$db->sql_fetchrow($result);
					$db->sql_freeresult($result);*/
					if($torrents[$torrent_id]['lastseedreq'] > $user->data['user_last_seedreq'] || $user->data['user_last_seedreq'] == $torrents[$torrent_id]['lastseedreq'])
					{
						$d_seedreq=$torrents[$torrent_id]['lastseedreq'];
					}
					else
					{
						$d_seedreq=$user->data['user_last_seedreq'];
					}
					if($dt - $d_seedreq < $ppkbb_srreq_time)
					{
						//$d_seedreq=1;
					}
					else
					{
						$d_seedreq=0;
					}
				}
				if(!$d_seedreq)
				{
					$seed_request=request_var('seed_request', 0);
					//!$seed_request ? $seed_request=(request_var('submit_seed_request_x', '') || request_var('submit_seed_request_y', '')) : '';
					if($seed_request)
					{
						$ppkbb_sruser_lvisit=intval($config['ppkbb_sruser_lvisit'] * 24 * 60 * 60);
						$ppkbb_srexcl_ratio=floatval($config['ppkbb_srexcl_ratio']);
						$sql="SELECT u.username, u.user_id FROM ". TRACKER_SNATCHED_TABLE .' s LEFT JOIN '.TRACKER_PEERS_TABLE.' p ON (p.userid=s.userid AND p.torrent=s.torrent) LEFT JOIN '. USERS_TABLE ." u ON (s.userid=u.user_id) LEFT JOIN ".SESSIONS_TABLE." st ON (u.user_id=st.session_user_id) WHERE s.torrent='{$torrent_id}'".($ppkbb_sruser_lvisit ? " AND IF(st.session_user_id, '{$dt}' - st.session_time < '{$ppkbb_sruser_lvisit}', '{$dt}' - u.user_lastvisit < '{$ppkbb_sruser_lvisit}')" : '').($ppkbb_srexcl_ratio ? " AND (s.uploaded='0' OR s.uploaded/s.downloaded < '{$ppkbb_srexcl_ratio}')" : '')." AND ISNULL(p.torrent) AND s.finished!='0'".($config['ppkbb_srusers_limit'] ? " ORDER BY RAND() LIMIT {$config['ppkbb_srusers_limit']}" : '');
						$result=$db->sql_query($sql);
						$leaves=array(
							'from_user_id'=>$user->data['user_id'],
							'icon_id'=>0,
							'from_user_ip'=>$_SERVER['REMOTE_ADDR'],
							'from_username'=>$user->data['username'],
							'enable_bbcode'=>0,
							'enable_smilies'=>0,
							'enable_urls'=>0,
							'enable_sig'=>0,
							'message'=>sprintf($user->lang['REQUEST_SEEDS'], "{$ajax_root_path}viewtopic.{$phpEx}?f={$forum_id}&amp;t={$topic_id}", $topic_data['topic_title']),
							'bbcode_bitfield'=>0,
							'bbcode_uid'=>0,
							'to_address'=>'',
							'bcc_address'=>'',
							);
						$leaves['address_list']['u'][$poster_id]='to';
						while($userlist=$db->sql_fetchrow($result))
						{
							$leaves['address_list']['u'][$userlist['user_id']]='to';
						}
						$db->sql_freeresult($result);
						if(@$leaves['address_list']['u'])
						{
							include_once($phpbb_root_path.'includes/functions_privmsgs.'.$phpEx);
							$db->sql_query("INSERT INTO ".TRACKER_SEEDREQUEST_TABLE." (user_id, torrent, added) VALUES('{$user->data['user_id']}', '{$torrent_id}', '{$dt}')");
							submit_pm('post', $user->lang['REQUEST_SEEDS_SUBJECT'], $leaves, false);
							$db->sql_query("UPDATE ".USERS_TABLE." SET user_fromseedreq_count=user_fromseedreq_count+1, user_last_seedreq='{$dt}' WHERE user_id='{$user->data['user_id']}'");
							$db->sql_query("UPDATE ".USERS_TABLE." SET user_toseedreq_count=user_toseedreq_count+1 WHERE user_id IN('".implode("', '", array_keys($leaves['address_list']['u']))."')");
							$db->sql_query("UPDATE ".TRACKER_TORRENTS_TABLE." SET lastseedreq='{$dt}' WHERE id='{$torrent_id}'");
							show_result($user->lang['TORR_SEEDREQ_SEND'], $opt);
						}
					}
				}
				else
				{
					show_result($user->lang['REQUEST_SEEDS_NEXT'].': '.get_time_diff($dt, $d_seedreq+$ppkbb_srreq_time), $opt);
				}
			}
			else
			{
				show_result('Invalid seed request');
			}
		}
		else
		{
			show_result('Invalid permissions (Seed request)');
		}
	break;

	case 'set_sticky':
		$is_cansetsticky = $auth->acl_get('f_sticky', $forum_id) ? 1 : 0;
		//$ppkbb_portal_torrents_posttime=my_split_config($config['ppkbb_portal_torrents_posttime'], 3, array('intval', 'my_int_val', 'my_int_val'));
		if(/*$ppkbb_portal_torrents_posttime[0] >= 0 && */$is_cansetsticky && $torrents[$torrent_id]['forb'] < 1)
		{
			$set_sticky=request_var('set_sticky', 0);
			//!$set_sticky ? $set_sticky=(request_var('submit_set_sticky_x', '') || request_var('submit_set_sticky_y', '')) : '';
			if($set_sticky)
			{
				$as_sticky=request_var('as_sticky', 0);
				$tlimit_sticky=request_var('torrent_time_limit', 0);
				$tlimit_sticky > 999 || $tlimit_sticky < 0 ? $tlimit_sticky=0 : $tlimit_sticky=$tlimit_sticky * 86400;
				if($as_sticky && $torrent_link)
				{
					$topic_data['topic_type']=POST_STICKY;
				}
				else
				{
					$topic_data['topic_type']=POST_NORMAL;
					$tlimit_sticky=0;
				}
				$topic_data['topic_time_limit']=$tlimit_sticky;
				$result=$db->sql_query('UPDATE '. TOPICS_TABLE ." SET topic_time_limit='".$tlimit_sticky."', topic_type='".$topic_data['topic_type']."' WHERE topic_id='{$topic_data['topic_id']}'");
				$left_sticky=0;
				if($topic_data['topic_time_limit'] && ($topic_data['topic_time'] + $topic_data['topic_time_limit']) > $dt)
				{
					$left_sticky=get_time_diff($dt, $topic_data['topic_time'] + $topic_data['topic_time_limit']);
				}
				$sticky_addit='';
				$sticky_addit.='
					<replaceContent select="#sticky_left">';
				$left_sticky ? $sticky_addit.=$user->lang['STICKY_EXPIRE'].': '.$left_sticky : '';
				$sticky_addit.='
					</replaceContent>
				';
				show_result($user->lang['TORR_STICKY_SET'], $opt, $sticky_addit);
			}
		}
		else
		{
			show_result('Invalid permissions (Sticky)');
		}
	break;

	case 'set_forb':
		$is_cansetforb = ($auth->acl_get('u_cansetforb') && $auth->acl_get('f_cansetforb', $forum_id)) ? 1 : 0;
		$is_cansetstatus = ($auth->acl_get('u_cansetstatus') && $auth->acl_get('f_cansetstatus', $forum_id)) ? 1 : 0;
		if($is_cansetforb || $is_cansetstatus)
		{
			$user->add_lang('mods/ppkbb3cker_statuses');
			$set_forb=request_var('set_forb', 0);
			//!$set_forb ? $set_forb=(request_var('submit_set_forb_x', '') || request_var('submit_set_forb_y', '')) : '';
			$forb_val=request_var('forb_val', 0);
			!isset($user->lang['TRACKER_FORB_MARK'][$forb_val]) || $forb_val > 99 || $forb_val < -99 ? $forb_val=0 : '';
			!$is_cansetforb && ($forb_val > -50 || $torrents[$torrent_id]['forb'] > 0) ? $set_forb=0 : '';
			!$is_cansetstatus && $forb_val < -49 ? $set_forb=0 : '';
			$forb_locked=request_var('forb_locked', 0) ? 1 : 0;
			$forb_closed=request_var('forb_closed', 0) ? 1 : 0;
			$forb_val==1 ? $forb_locked=1 : '';
			$forb_val==0 ? $forb_locked=0 : '';
			if($set_forb)
			{
				$forb_reason=request_var('forb_reason', '') ? utf8_normalize_nfc(request_var('forb_reason', '', true)) : '';
				$result=$db->sql_query('UPDATE '. POSTS_TABLE ." SET post_edit_locked='{$forb_locked}' WHERE post_id='{$post_id}'");
				$result=$db->sql_query('UPDATE '. TOPICS_TABLE ." SET topic_status='{$forb_closed}' WHERE topic_id='{$topic_id}'");
				$result=$db->sql_query('UPDATE '. TRACKER_TORRENTS_TABLE ." SET forb='{$forb_val}', forb_reason='".$db->sql_escape(truncate_string($forb_reason, 255, 255, false))."', forb_date='{$dt}', forb_user_id='{$user->data['user_id']}' WHERE post_msg_id='{$post_id}'");
				$ppkbb_tstatus_notify=my_split_config($config['ppkbb_tstatus_notify'], 2, 'my_int_val');
				if($user->data['user_id']!=$poster_id && (($ppkbb_tstatus_notify[0] && $forb_val < 1 && $torrents[$torrent_id]['forb'] > 0) || ($ppkbb_tstatus_notify[1] && $forb_val  > 0 && $torrents[$torrent_id]['forb'] < 1)))
				{
					$approve=array(
						'from_user_id'=>$user->data['user_id'],
						'icon_id'=>0,
						'from_user_ip'=>$_SERVER['REMOTE_ADDR'],
						'from_username'=>$user->data['username'],
						'enable_bbcode'=>0,
						'enable_smilies'=>0,
						'enable_urls'=>0,
						'enable_sig'=>0,
						'message'=>sprintf($user->lang['TORRENT_NOTIFY_TEXT'], append_sid($phpbb_root_path."viewtopic.{$phpEx}?f={$forum_id}&amp;t={$topic_id}"), $topic_data['topic_title'], $user->lang['TRACKER_FORB_REASON'][$forb_val], $user->lang['TRACKER_FORB_REASON'][$torrents[$torrent_id]['forb']], get_username_string('full', $user->data['user_id'], $user->data['username'], $user->data['user_colour']), htmlspecialchars($forb_reason), date('Y-m-d H:i:s', $dt)),
						'bbcode_bitfield'=>0,
						'bbcode_uid'=>0,
						'to_address'=>'',
						'bcc_address'=>'',
					);
					$approve['address_list']['u'][$poster_id]='to';
					if(isset($approve['address_list']['u']))
					{
						include_once($phpbb_root_path.'includes/functions_privmsgs.'.$phpEx);
						submit_pm('post', $user->lang['TORRENT_NOTIFY_SUBJECT'], $approve, false);
					}
				}
				$torrents[$torrent_id]['forb_reason']=$forb_reason;
				$torrents[$torrent_id]['forb_user_id']=$user->data['user_id'];
				$torrents[$torrent_id]['username']=$user->data['username'];
				$torrents[$torrent_id]['user_colour']=$user->data['user_colour'];
				$torrents[$torrent_id]['forb_date']=$dt;
				$torrents[$torrent_id]['forb']=$forb_val;

				$forb_addit='';
				$forb_addit.='
					<replaceContent select="#c_tstatus">';
				$torrents[$torrent_id]['forb']!=0 ? $forb_addit.='<strong>'.$user->lang['TORRENT_STATUS'].'</strong>: '.$user->lang['TRACKER_FORB_MARK'][$torrents[$torrent_id]['forb']] : $forb_addit.='';
				$forb_addit.='
					</replaceContent>
				';
				$forb_addit.='
					<replaceContent select="#c_ustatus">';
				$torrents[$torrent_id]['forb_user_id'] ? $forb_addit.='<strong>'.$user->lang['TORRENT_STATUS_USER'].'</strong>: '.str_replace('../', ($adm ? './../' : './'), get_username_string('full', $torrents[$torrent_id]['forb_user_id'], $torrents[$torrent_id]['username'], $torrents[$torrent_id]['user_colour'])).' ('.$user->format_date($torrents[$torrent_id]['forb_date']).')' : $forb_addit.='';
				$forb_addit.='
					</replaceContent>
				';
				$forb_addit.='
					<replaceContent select="#c_treason">';
				$torrents[$torrent_id]['forb_reason'] ? $forb_addit.='<strong>'.$user->lang['TORRENT_STATUS_REASON'].'</strong>: '.$torrents[$torrent_id]['forb_reason'] : $forb_addit.='';
				$forb_addit.='
					</replaceContent>
				';
				show_result($user->lang['TORR_FORB_CHANGED'], $opt, $forb_addit);
			}
		}
		else
		{
			show_result('Invalid permissions (Forb)');
		}
	break;

	default:
		show_result('Invalid opt');
	break;
}

exit();

//##############################################################################
function show_result($str, $opt='set_error', $addit='')
{
	$opt!==false ? header('Content-type: text/xml; charset=UTF-8') : header('Content-type: text/html; charset=UTF-8');
	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
	header("Cache-Control: no-cache, must-revalidate");
	header("Cache-Control: post-check=0,pre-check=0", false);
	header("Cache-Control: max-age=0", false);
	header("Pragma: no-cache");

	if($opt===false)
	{
		echo $str;
		return '';
	}

	if($opt!='set_error')
	{
		$addit.='
		<replaceContent select="#c_set_error">
		</replaceContent>
		';
	}

	echo '
	<taconite>
		<replaceContent select="#c_'.$opt.'">
			'.$str.'
		</replaceContent>
		'.$addit.'
		<replace select="#hj_wait">
		</replace>
	</taconite>
	';

	exit();
}
?>
